import 'persion.dart';
import 'dart:math';
// import 'dart:io';
import 'package:http/http.dart' as http;

/* 开发调试终端命令:
r键： 点击后热加载
R键： 热重启项目
p键： 显示调试界面的网格，可以很好的掌握布局情况
o键： 切换Android和iOS的预览模式
q键：退出调试预览模式
*/

void main() {
  print(getData(1));

  print(getData<String>('泛型'));

  Persion persion = Persion('泛型', 16);
  Test test = Test<Persion>(persion);
  test.pringInfo();

  print(max(12, 3));

  httpRequest();

  print('"str!"类型断言');
  String? str = 'this is str';
  print(str.length);
  str = null;
  print('str: ${str?.length}');
  // str!.length
  try {
    print(str!.length);
  } catch (e) {
    print('str = null: $e');
  }

  // 共享存储空间：1、常量2、值相等
  Container container1 = const Container(width: 1, height: 1);
  Container container2 = const Container(width: 1, height: 1);
  print(
      'identical(container1, container2): ${identical(container1, container2)}'); // true
}

// 泛型
T getData<T>(T value) {
  return value;
}

class Test<T> {
  T name;
  Test(this.name);

  pringInfo() {
    if (name is Persion) {
      (name as Persion).printUserInfor();
    } else {
      print(name);
    }
  }
}

void httpRequest() async {
  var url = Uri.https('example.com', 'whatsit/create');
  var response =
      await http.post(url, body: {'name': 'doodle', 'color': 'blue'});
  print('Response status: ${response.statusCode}');
  print('Response body: ${response.body}');

  print(await http.read(Uri.https('example.com', 'foobar.txt')));
}

// 常量构造函数
class Container {
  final int width;
  final int height;

  const Container({required this.width, required this.height});
}
